package com.zh.tea.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.UserMapper;
import com.zh.tea.entity.User;
import com.zh.tea.entity.UserExample;
import com.zh.tea.service.UserService;
import com.zh.tea.utils.SessionFactoryUtils;



public class UserServiceImpl implements UserService{
	
	/**
	 * 用用户名或者电话或者邮箱登录
	 * @param name
	 * @param pwd
	 * @return
	 */
		public User login(String name ,String pwd){
			SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
			UserMapper um=session.getMapper(UserMapper.class);
			UserExample uename=new UserExample();
			//按用户名查找
			UserExample.Criteria criterianame=uename.createCriteria();
			criterianame.andPassnameEqualTo(name);
			criterianame.andPasswordEqualTo(pwd);
			List<User> username=um.selectByExample(uename);
			if(!username.isEmpty()){
				session.close();
				return username.get(0);
			}
			/*
			//按邮箱查找
			UserExample ueemail=new UserExample();
			UserExample.Criteria criteriaemail=ueemail.createCriteria();
			criteriaemail.andEmailEqualTo(name);
			criteriaemail.andPasswordEqualTo(pwd);
			List<User> usereamil=um.selectByExample(ueemail);
			if(!usereamil.isEmpty()){
				session.close();
				return usereamil.get(0);
			}
			//按电话查找
			UserExample uephone=new UserExample();
			UserExample.Criteria criteriaphone=uephone.createCriteria();
			criteriaphone.andPhoneEqualTo(name);
			criteriaphone.andPasswordEqualTo(pwd);
			List<User> uphone=um.selectByExample(uephone);
			if(!uphone.isEmpty()){
				session.close();
				return uphone.get(0);
			}*/
			session.close();
			return null;
		}
	/**
	 * 增加用户
	 */
	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub	
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		UserMapper um=session.getMapper(UserMapper.class);
		
		int a=um.insertSelective(user);
		session.commit();
		session.close();
		return a;
	}
/**
 * 删除用户
 */
	@Override
	public int delUser(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		UserMapper um=session.getMapper(UserMapper.class);
		int a = um.deleteByPrimaryKey(id);
		session.commit();
		session.close();
		return a;
	}
	/**
	 * 修改用户
	 */
@Override
public int updateUser(User user) {
	// TODO Auto-generated method stub
	SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
	UserMapper um=session.getMapper(UserMapper.class);
	int a = um.updateByPrimaryKey(user);
	session.commit();
	session.close();
	return a;
}
	@Override
	public PageInfo<User> getUserByPage(User user, Integer pagNo, Integer PageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		UserMapper um=session.getMapper(UserMapper.class);
		UserExample uename=new UserExample();
		UserExample.Criteria criterianame=uename.createCriteria();//如果user不为空将按条件查询
		if(user!=null){
			if(user.getId()!=null){
				criterianame.andIdEqualTo(user.getId());
			}
			if(user.getPassname()!=null){
				criterianame.andPassnameEqualTo(user.getPassname());
			}
			if(user.getPassword()!=null){
				criterianame.andPasswordEqualTo(user.getPassword());
			}
		}
		PageHelper.startPage(pagNo, PageSize);
		List<User> username=um.selectByExample(uename);
		PageInfo<User> pagetea=new PageInfo<>(username);
		return pagetea;
	}
	@Override
	public User showinfo(int id) {//在用户个人中心展示个人信息
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		UserMapper um=session.getMapper(UserMapper.class);
		UserExample uid=new UserExample();
		User user=null;
		//按id查找
		UserExample.Criteria criteriauid=uid.createCriteria();
		criteriauid.andIdEqualTo(id);
		List<User> info=um.selectByExample(uid);
		if(!info.isEmpty()) {
			session.close();
			return info.get(0);
		}
		return null;
	}
	@Override
	public int updateUserInfo(User user,Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		UserMapper um=session.getMapper(UserMapper.class);
		int a =um.updateByPrimaryKeySelective(user);
		session.commit();
		session.close();
		return a;
	}
	@Override
	public User selectOne(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		UserMapper um=session.getMapper(UserMapper.class);
		User user=um.selectByPrimaryKey(id);
		session.commit();
		session.close();
		return user;
	}
	
	
}
